package com.naire.modul.answer.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import net.common.dao.GeneralDaoImpl;
import net.common.service.PageBean;
import net.common.util.BeanUtils;

import com.naire.modul.answer.dao.AnswerDao;
import com.naire.modul.answer.pojo.Answer;
import com.naire.modul.answer.pojo.Option;
import com.naire.modul.answer.vo.AnswerVo;
import com.naire.modul.answer.vo.OptionVo;

public class AnswerDaoImpl extends GeneralDaoImpl implements AnswerDao {

	@SuppressWarnings("unchecked")
	@Override
	public List<AnswerVo> search(PageBean pageBean, String parent_code) throws Exception {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("sort_num", parent_code);
		StringBuilder hql = new StringBuilder("from Answer where flag=0 and sort_num=:sort_num");
		List<AnswerVo> d = new ArrayList<AnswerVo>();
		List<Answer> l = hibernateHandler.query(hql, pageBean, params);
		for(Answer bean : l) {
			AnswerVo dv = new AnswerVo();
			BeanUtils.copyProperties(bean, dv);
			d.add(dv);
		}
		return d;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<AnswerVo> searchAll(PageBean pageBean, String parent_code) throws Exception {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("sort_num", parent_code);
		StringBuilder hql = new StringBuilder("from Answer where flag=0 and sort_num=:sort_num");
		List<AnswerVo> d = new ArrayList<AnswerVo>();
		List<Answer> l = hibernateHandler.query(hql, pageBean, params);
		for(Answer bean : l) {
			AnswerVo dv = new AnswerVo();
			BeanUtils.copyProperties(bean, dv);
			Iterator<Option> iterator = bean.getOptions().iterator();
			while(iterator.hasNext()) {
				Option o = iterator.next();
				OptionVo v = new OptionVo();
				BeanUtils.copyProperties(o, v);
				dv.getOptions().add(v);
			}
			d.add(dv);
		}
		return d;
	}

	@Override
	public void insert(AnswerVo bean) throws Exception {
		Answer pojo = new Answer();
		BeanUtils.copyProperties(bean, pojo);
		for(OptionVo vo : bean.getOptions()) {
			if(vo != null) {				
				Option option = new Option();
				BeanUtils.copyProperties(vo, option);
				option.setAnswer(pojo);
				pojo.getOptions().add(option);
			}
		}
		hibernateHandler.insert(pojo);
	}

	@Override
	public void update(AnswerVo bean) throws Exception {
		Answer pojo = (Answer)hibernateHandler.find(Answer.class, bean.getAnswer_id());
		BeanUtils.copyProperties(bean, pojo);
		pojo.getOptions().clear();
		for(OptionVo vo : bean.getOptions()) {
			if(vo != null) {				
				Option option = new Option();
				BeanUtils.copyProperties(vo, option);
				option.setAnswer(pojo);
				pojo.getOptions().add(option);
			}
		}
		hibernateHandler.update(pojo);
	}

	@Override
	public void updateFlag(AnswerVo bean) throws Exception {
		update(bean);
	}

	@Override
	public AnswerVo find(Integer id) throws Exception {
		Answer pojo = (Answer)hibernateHandler.find(Answer.class, id);
		AnswerVo bean = new AnswerVo();
		BeanUtils.copyProperties(pojo, bean);
		Iterator<Option> sets = pojo.getOptions().iterator();
		while(sets.hasNext()) {
			OptionVo v = new OptionVo();
			BeanUtils.copyProperties(sets.next(), v);
			bean.getOptions().add(v);
		}
		return bean;
	}
}
